package com.minimalist.gateway.config;

import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import reactor.core.publisher.Mono;

import java.util.Objects;

/**
 * 限流配置
 *
 * @author minimalist
 */
@Configuration
public class RateLimiterConfig{

    /**
     * 基于IP地址的限流
     */
    @Bean
    public KeyResolver ipKeyResolver(){
        return exchange -> {
            String ip = Objects.requireNonNull(exchange.getRequest()
                    .getRemoteAddress()).getHostString();
            return Mono.just(ip);
        };
    }
}